package com.detrum.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.detrum.bean.ViewPagerItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by abc on 2017/7/19.
 */

public class SqliteDeviceOperate {
    private static SqliteDeviceOperate operate;
    private SQLiteDatabase sqLiteWrite, sqLiteRead;
    private final String tableName = "Device_info";
    private final String DeviceName = "name";
    private final String DeviceImagePath = "imagePath";
    private final String DeviceImage = "image";
    private final String DeviceMsg = "msg";
    private final String DevicePrice = "price";
    private final String DeviceMarketTime = "marketTime";

    private SqliteDeviceOperate(Context context) {
        sqLiteWrite = SqliteHelper.getInstance(context).getWritableDatabase();
        sqLiteRead = SqliteHelper.getInstance(context).getReadableDatabase();
    }

    public static SqliteDeviceOperate getInstance(Context context) {
        if (operate == null) {
            operate = new SqliteDeviceOperate(context);
        }
        synchronized (operate) {
            return operate;
        }
    }

    public long Insert(ViewPagerItem viewPagerItem){
        int count = 0;
        if(viewPagerItem==null){
            return -1;
        }
        if(viewPagerItem.getName()==null||viewPagerItem.getName().length()==0){
            return -2;
        }
        count = SelectSingle(viewPagerItem.getName());
        if(count>0){
            int update = Updata(viewPagerItem);
            return update;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(DeviceName,viewPagerItem.getName());
        contentValues.put(DeviceMsg,viewPagerItem.getMsg());
        contentValues.put(DeviceImage,viewPagerItem.getImage());
        contentValues.put(DeviceImagePath,viewPagerItem.getImagePath());
        contentValues.put(DevicePrice,viewPagerItem.getPrice());
        contentValues.put(DeviceMarketTime,viewPagerItem.getMarketTime());
        long insert = sqLiteWrite.insert(tableName,null,contentValues);
        return insert;
    }

    public int Delete(String Name){
        int delete = sqLiteWrite.delete(tableName,DeviceName + "=?",new String[]{Name});
        return delete;
    }

    public int Updata(ViewPagerItem viewPagerItem){
        int count = 0;
        if(viewPagerItem==null){
            return -1;
        }
        if(viewPagerItem.getName()==null){
            return -2;
        }
        count = SelectSingle(viewPagerItem.getName());
        if(count==0){
            return -3;
        }
        ContentValues contentValues = new ContentValues();
        if(viewPagerItem.getMsg()!=null){
            contentValues.put(DeviceMsg,viewPagerItem.getMsg());
        }
        if(viewPagerItem.getImage()!=0){
            contentValues.put(DeviceImage,viewPagerItem.getImage());
        }
        if(viewPagerItem.getImagePath()!=null){
            contentValues.put(DeviceImagePath,viewPagerItem.getImagePath());
        }
        if(viewPagerItem.getPrice()!=0){
            contentValues.put(DevicePrice,viewPagerItem.getPrice());
        }
        if(viewPagerItem.getMarketTime()!=null){
            contentValues.put(DeviceMarketTime,viewPagerItem.getMarketTime());
        }
        int updata = sqLiteWrite.update(tableName,contentValues,DeviceName + "=?",new String[]{viewPagerItem.getName()});
        return updata;
    }

    public List<ViewPagerItem>SelectAll(){
        Cursor cursor = sqLiteRead.query(tableName,null,null,null,null,null,DeviceName + " ASC");
        List<ViewPagerItem>list=null;
        if(cursor.getCount()>0){
            list = new ArrayList<>();
            while (cursor.moveToNext()){
                String name = cursor.getString(cursor.getColumnIndex(DeviceName));
                String msg = cursor.getString(cursor.getColumnIndex(DeviceMsg));
                String imagePath = cursor.getString(cursor.getColumnIndex(DeviceImagePath));
                String marketTime = cursor.getString(cursor.getColumnIndex(DeviceMarketTime));
                int image = cursor.getInt(cursor.getColumnIndex(DeviceImage));
                double price = cursor.getDouble(cursor.getColumnIndex(DevicePrice));
                ViewPagerItem item = new ViewPagerItem(name,msg,image,imagePath,price,marketTime);
                list.add(item);
            }
        }
        return list;
    }

    public ViewPagerItem Select(String filename){
        ViewPagerItem item = null;
        Cursor cursor = sqLiteRead.query(tableName,null,DeviceName+" = ?",new String[]{filename},null,null,DeviceName + " ASC");
        if(cursor.moveToFirst()){
            String name = cursor.getString(cursor.getColumnIndex(DeviceName));
            String msg = cursor.getString(cursor.getColumnIndex(DeviceMsg));
            String imagePath = cursor.getString(cursor.getColumnIndex(DeviceImagePath));
            String marketTime = cursor.getString(cursor.getColumnIndex(DeviceMarketTime));
            int image = cursor.getInt(cursor.getColumnIndex(DeviceImage));
            double price = cursor.getDouble(cursor.getColumnIndex(DevicePrice));
            item = new ViewPagerItem(name,msg,image,imagePath,price,marketTime);
        }
        return item;
    }

    public int SelectSingle(String name){
        Cursor cursor = sqLiteRead.query(tableName,null,DeviceName+"=?",new String[]{name},null,null,null);
        return cursor.getCount();
    }
}
